package flc.ast.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.FileTransferActivity;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.bean.MyImgBean;
import flc.ast.bean.SelectFileBean;
import flc.ast.databinding.FragmentTransferPicBinding;
import flc.ast.utils.MhzDateUtils;
import flc.ast.utils.SendFileManager;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.UriUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TransferPicFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TransferPicFragment extends BaseNoModelFragment<FragmentTransferPicBinding> {

    private SelPictureAdapter selPictureAdapter;
    private SendFileManager mInstance;

    @Override
    protected int onCreate() {
        return R.layout.fragment_transfer_pic;
    }

    @Override
    protected void initView() {
        mInstance = SendFileManager.getInstance();

        mDataBinding.rvPictureList.setLayoutManager(new LinearLayoutManager(mContext));
        selPictureAdapter = new SelPictureAdapter();
        selPictureAdapter.setEdit(true);
        mDataBinding.rvPictureList.setAdapter(selPictureAdapter);
        selPictureAdapter.setOnItemClickListener(this);
        selPictureAdapter.setClickFeedback(new SelPictureAdapter.ClickFeedback() {
            @Override
            public void onClickFeedback(boolean isSel, String path) {
                boolean hasExist = false;
                int tmpPosition = -1;
                for (SelectFileBean videoCollectionBean : mInstance.getSelectFileBeans()) {
                    tmpPosition++;
                    if (UriUtil.getImageUri(mContext, path).toString().equals(videoCollectionBean.getPath())) {
                        hasExist = true;
                        break;
                    }
                }
                if (hasExist) {
                    mInstance.deleteSelectFile(tmpPosition);
                } else {
                    SelectFileBean bean = new SelectFileBean();
                    bean.setType(1);
                    bean.setName(FileUtils.getFileName(path));
                    bean.setPath(UriUtil.getImageUri(mContext, path).toString());
                    bean.setSize(FileUtils.getLength(path));
                    mInstance.addSelectFile(bean);
                }
            }
        });
    }

    @Override
    protected void initData() {
        getPermission();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_pic_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvPictureList.setVisibility(View.GONE);
                        mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        showDialog(getString(R.string.loading));
        RxUtil.create(new RxUtil.Callback<List<MyImgBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<MyImgBean>> emitter) {
                List<String> list = new ArrayList<>();
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                for (SelectMediaEntity bean : listSys) {
                    list.add(bean.getPath());
                }
                List<MyImgBean> dayData = MhzDateUtils.getDayData(list);
                emitter.onNext(dayData);
            }

            @Override
            public void accept(List<MyImgBean> list) {
                dismissDialog();
                if (list != null && list.size() > 0) {
                    selPictureAdapter.setList(list);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                    mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}